﻿using System;
using System.Collections.Generic;
using System.Collections;

namespace Diary.BAL
{
    public class Setting
    {
        const string TableName = "Setting";

        /// <summary>
        /// 向数据库中插入数据，如成功返回新记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        static public Model.Setting Insert(Model.Setting model)
        {
            string sql = string.Format("insert into {0} (Key, Value) values ('{1}', '{2}')", TableName, model.Key, model.Value);
            model.Id = DBController.Default.executeReturnLastId(sql);
            return model;
        }

        /// <summary>
        /// 删除指定ID的记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        static public bool Delete(int id)
        {
            string sql = string.Format("delete from {0} where Id={1}", TableName, id);
            return DBController.Default.executeNoReturn(sql);
        }


        /// <summary>
        /// 修改指定ID的记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        static public bool Modify(Model.Setting model)
        {
            string sql = string.Format("update {0} set Key='{2}', Value='{3}' where Id={1}", TableName, model.Id, model.Key, model.Value);
            return DBController.Default.executeNoReturn(sql);
        }

        static public bool ModifyByKey(string key, string value)
        {
            string sql = string.Format("update {0} set Value='{2}' where Key='{1}'", TableName, key, value);
            return DBController.Default.executeNoReturn(sql);
        }

        /// <summary>
        /// 返回所有记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        static public List<Model.Setting> query_all()
        {
            string sql = string.Format("select * from {0}", TableName);
            List<Hashtable> results = DBController.Default.query(sql);
            List<Model.Setting> history = new List<Model.Setting>();
            foreach (Hashtable result in results)
            {
                Model.Setting model = new Model.Setting();
                model.Id = int.Parse(result["Id"].ToString());
                model.Key = result["Key"].ToString();
                model.Value = result["Value"].ToString();
                history.Add(model);
            }

            return history;
        }

        static Hashtable htValues = new Hashtable();
        static public bool update_value(string key, string value)
        {
            if (htValues.ContainsKey(key))
            {
                htValues[key] = value;
                return ModifyByKey(key, value);
            }
            else
            {
                htValues.Add(key, value);
                Model.Setting model = new Model.Setting() { Key = key, Value = value };
                return Insert(model) != null;
            }
        }
        static public string get_string_value(string key)
        {
            if (htValues.Count == 0)
            {
                List<Model.Setting> models = query_all();
                foreach (Model.Setting model in models)
                {
                    if (htValues.Contains(model.Key)) continue;
                    htValues[model.Key] = model.Value;
                }
            }
            return htValues.toString(key);
        }

        static public bool get_bool_value(string key)
        {
            return (get_string_value(key) == "1");
        }
        static public bool update_bool_value(string key, bool value)
        {
            return update_value(key, value ? "1" : "0");
        }
    }
}
